//index.js
var app = getApp();
Page({
  data:{
    // text:"这是一个页面"
    recommendedIndex: 4,
    reachBottomStatus: 1,
    emptyStatus: 1,
    lastId: 0,
    goodsItem:{},
    commentList: []
  },
  onLoad:function(options){
    // 页面初始化 options为页面跳转所带来的参数

  },
  onReady:function(){
    // 页面渲染完成
    this.loadData(this.data.lastId);
  },
  onShow:function(){
    // 页面显示
  },
  onReachBottom: function() {
    // Do something when page reach bottom.
    var that = this;
    if(that.data.reachBottomStatus == 2){
      var lastId = that.data.commentList[that.data.commentList.length-1].id;
      that.loadData(lastId);
    }
  },
  onPullDownRefresh: function (){
    var that = this;
    that.setData({
      commentList: []
    });
    this.loadData(0);
    wx.stopPullDownRefresh();
  },
  loadData: function (id) {
    var that = this;
    app.doRequest('myCommentList', {
      nextId: id,
      pageSize: 10
    }, function (data) {


      var commentListItems = data;
      var commentLoadedListItems = that.data.commentList;


      if(commentListItems.length > 0){
        commentListItems.forEach(function(currentValue,index,arr){
          currentValue.createAt = app.datetime(currentValue.createAt);
          currentValue.goodNumber = parseInt(currentValue.goodNumber);
          currentValue.image10 = app.formatImage(currentValue.image1,[128,128]);
          currentValue.image20 = app.formatImage(currentValue.image2,[128,128]);
          currentValue.image30 = app.formatImage(currentValue.image3,[128,128]);
          currentValue.image40 = app.formatImage(currentValue.image4,[128,128]);
          currentValue.image50 = app.formatImage(currentValue.image5,[128,128]);
          currentValue.image11 = app.formatImage(currentValue.image1,[640,0]);
          currentValue.image22 = app.formatImage(currentValue.image2,[640,0]);
          currentValue.image33 = app.formatImage(currentValue.image3,[640,0]);
          currentValue.image44 = app.formatImage(currentValue.image4,[640,0]);
          currentValue.image55 = app.formatImage(currentValue.image5,[640,0]);
          commentLoadedListItems.push(currentValue);
        });
        that.setData({
          commentList: commentLoadedListItems,
        });

        that.data.reachBottomStatus = 2;
      }else{
        that.data.reachBottomStatus = 1;
      }
      if(that.data.commentList.length > 0){
        that.setData({
          emptyStatus: 1
        });
      }else{
        that.setData({
          emptyStatus: 2
        });
      }


    })
  },
  gotoDetail: function (event) {
    //console.log(event);
    var goodsId =event.currentTarget.dataset.goodsid;
    //console.log(goodsId);
    wx.navigateTo({ url: '/pages/product/detail/index?Id='+ goodsId +'' });
  },
  deleteCommentItem: function(event){
    var that = this;
    var commentId =event.currentTarget.dataset.commentid;
    var commentIndex =event.currentTarget.dataset.idx;
    var commentLoadedListItems = that.data.commentList;
    wx.showModal({
      title: '提示',
      content: '是否删除评论？',
      success: function(res) {
        if (res.confirm) {
          app.doRequest('myCommentList_Delete', {
            commentId: commentId
          }, function (data) {
            commentLoadedListItems.splice(commentIndex,1);
            that.setData({
              commentList: commentLoadedListItems
            });
            if(that.data.commentList.length > 0){
              that.setData({
                emptyStatus: 1
              });
            }else{
              that.setData({
                emptyStatus: 2
              });
            }
          })
        }
      }
    })


  },
  previewImageComment: function (event){
    var that = this;
    var commentId = event.currentTarget.dataset.commentid;
    var currentUrl = event.currentTarget.dataset.url;
    var commentIndex = parseInt(event.currentTarget.dataset.idx);
    var commentListAllItems = that.data.commentList;
    var currentComment = that.data.commentList[commentIndex];
    var previewImageAll = [];
    currentComment.image11 ? previewImageAll.push(currentComment.image11) : '';
    currentComment.image22 ? previewImageAll.push(currentComment.image22) : '';
    currentComment.image33 ? previewImageAll.push(currentComment.image33) : '';
    currentComment.image44 ? previewImageAll.push(currentComment.image44) : '';
    currentComment.image55 ? previewImageAll.push(currentComment.image55) : '';

    wx.previewImage({
      current: currentUrl, // 当前显示图片的http链接
      urls: previewImageAll // 需要预览的图片http链接列表
    })
  }
})